package com.aft.terra.common.utils;

import java.text.ParseException;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.aft.terra.common.bean.DateBean;
import com.aft.terra.modules.basic.SystemConfiguration;
import com.jfinal.plugin.activerecord.Db;

public class DateUtil {

	static SimpleDateFormat sdfhsm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 当前时间加任意几分钟
	 * 
	 * @author Mandarava
	 * @param minute
	 * @return
	 */
	public static Date addArbitrarilyMinute(int minute) {
		long currentTime = System.currentTimeMillis();
		currentTime += minute * 60 * 1000;
		Date date = new Date(currentTime);
		return date;
	}

	public static Date yesterDay(){
		Date today = new Date();
		Date yesterday = new Date(today.getTime() - 86400000L);//86400000L，它的意思是说1天的时间=24小时 x 60分钟 x 60秒 x 1000毫秒 单位是L。
		return yesterday;
	}
	
	public static String formatDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}

	public static String formatTime(String date) {
		return sdfhsm.format(date);
	}

	/**
	 * 参数(0,1)，就是当月第一天，(1,0)，就是当月最后一天,(1,1)就是下个月的1号
	 * 
	 * @param month
	 * @param day
	 * @return
	 */
	public static String getDay(Integer month, Integer day) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.MONTH, month);
		c.set(Calendar.DAY_OF_MONTH, day);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(c.getTime());
	}

	/**
	 * 返回当前时间毫秒
	 * 
	 * @author Mandarava
	 * @return
	 */
	public static Long nowTime() {
		return new Date().getTime();
	}

	/**
	 * 返回两个时间相差的秒数
	 * 
	 * @author Mandarava
	 * @param start
	 * @param end
	 * @return
	 */
	public static Integer minus(Long start, Long end) {
		if (start == null || end == null)
			return -1;
		if (end > start) {
			return (int) ((end - start) / 1000);
		} else if (start > end) {
			return (int) (start - end) / 1000;
		}
		return 0;
	}

	/**
	 * * 例如想获得2015-12-30的6个月后的日期，参数就是(0,6,0,2015-12-30)，返回的就是2016-06-30
	 * 如果参数day不为0，会在当前日期+day天，如果参数是(0,6,3,2015-12-30)，返回的结果就会是2016-07-03
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @param date
	 * @return
	 */
	public static String getDate(Integer year, Integer month, Integer day, Date date) {
		if (date == null) {
			date = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.YEAR, year);
		c.add(Calendar.MONTH, month);
		c.add(Calendar.DAY_OF_MONTH, day);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(c.getTime());
	}

	/**
	 * 传进来的date加对应的年月日
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @param date
	 * @return
	 */
	public static String addDay(Integer year, Integer month, Integer day, Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.YEAR, year);
		c.add(Calendar.MONTH, month);
		c.add(Calendar.DAY_OF_MONTH, day);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(c.getTime());
	}

	/**
	 * 获取今日开始时间
	 * 
	 * @return
	 */
	public static String nowDateBegin() {
		return "'" + now() + " 00:00:00'";
	}

	/**
	 * 获取今日结束时间
	 * 
	 * @return
	 */
	public static String nowDateEnd() {
		return "'" + now() + " 23:59:59'";
	}

	/**
	 * 系统当前事件格式为(yyyy-mm-dd)
	 * 
	 * @return
	 */
	public static String now() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}

	/**
	 * 系统当前事件格式为(yyyy-MM-dd HH:mm:ss)
	 * 
	 * @return
	 */
	public static String nowYMDHMS() {
		Date date = new Date();
		SimpleDateFormat sdfhsm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdfhsm.format(date);
	}

	public static String nowYMDHMSTwo(Date date) {
		SimpleDateFormat sdfhsm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdfhsm.format(date);
	}

	public static Date date() {
		return new Date();
	}

	public static java.sql.Timestamp sqlDate() {
		return new java.sql.Timestamp(date().getTime());
	}

	public static java.sql.Timestamp sqlTime(Date data) throws ParseException {
		return new java.sql.Timestamp(data.getTime());
	}

	public static java.sql.Timestamp sqlTime(String data) throws ParseException {
		SimpleDateFormat sdfhsm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return new java.sql.Timestamp(sdfhsm.parse(data).getTime());
	}

	public static java.sql.Timestamp sqlTimestamp() {
		return new java.sql.Timestamp(date().getTime());
	}

	/**
	 * 根据时间戳返回字符串
	 */
	public static String getTimestamp() {
		String formt = "yyyyMMddHHmmssSSS";
		SimpleDateFormat sdf = new SimpleDateFormat(formt);
		return sdf.format(new Date());
	}

	/**
	 * 根据 数据库函数返回字符串
	 */
	public static String nextval(String key) {
		Integer num = Db.queryInt("select nextval(?)", key);
		return num.toString();
	}

	/**
	 * 计算出两个时间相差多少天
	 * 
	 * @param smdate
	 *            开始时间
	 * @param bdate
	 *            结束时间
	 * @return
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate) {
		if (smdate == null || bdate == null)
			return 0;
		long between_days = (bdate.getTime() - smdate.getTime()) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	public static int getYear() {
		Calendar a = Calendar.getInstance();
		return a.get(Calendar.YEAR);
	}

	/**
	 * 字符串的日期格式的计算
	 */
	public static int daysBetween(String smdate, String bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		long between_days = (sdf.parse(bdate).getTime() - sdf.parse(smdate).getTime()) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * @author Mr.Ni
	 * @Title: findYearMonth
	 * @Description: 获取年月
	 * @return String
	 */
	public static String findYearMonth() {
		int year;
		int month;
		String date;
		Calendar calendar = Calendar.getInstance();
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH) + 1;
		date = year + "" + (month < 10 ? "0" + month : month);
		return date;
	}

	public static Date formatStringToDate(String source) {
		SimpleDateFormat sdfhsm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdfhsm.parse(source);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 获取周第一天
	 * 
	 * @return
	 */
	public static String fromatStringToDayOfWeek() {
		Calendar cDay = Calendar.getInstance();
		cDay.setTime(new Date());
		int week = cDay.get(Calendar.DAY_OF_WEEK) - 1;
		if (week == 0) { // 如果刚好是周日，直接返回
			cDay.add(Calendar.DAY_OF_YEAR, -7);
			cDay.set(Calendar.DAY_OF_WEEK, 2);
			return DateUtil.formatDate(cDay.getTime());
		} else {
			cDay.set(Calendar.DAY_OF_WEEK, 2);// 老外将周日定位第一天，周一取第二天
		}
		return DateUtil.formatDate(cDay.getTime());
	}

	/**
	 * 获取周最后一天
	 * 
	 * @return
	 */
	public static String fromatStringToFirstDayOfWeek() {
		Calendar cDay = Calendar.getInstance();
		int week = cDay.get(Calendar.DAY_OF_WEEK) - 1;
		if (week == 0) { // 如果刚好是周日，直接返回
			return DateUtil.formatDate(new Date());
		} else {// 如果不是周日，加一周计算
			cDay.set(Calendar.DAY_OF_WEEK, 2);// 老外将周日定位第一天，周一取第二天
			cDay.add(Calendar.DAY_OF_WEEK, 6);
			// cDay.set(Calendar.DAY_OF_WEEK, 1);
		}
		return DateUtil.formatDate(cDay.getTime());
	}

	public static void main(String[] args) {
		Calendar cDay = Calendar.getInstance();
		int week = cDay.get(Calendar.DAY_OF_WEEK) - 1;
		if (week == 0) { // 如果刚好是周日，直接返回
			System.out.println(DateUtil.formatDate(new Date()));
		} else {// 如果不是周日，加一周计算
			cDay.set(Calendar.DAY_OF_WEEK, 2);// 老外将周日定位第一天，周一取第二天
			cDay.add(Calendar.DAY_OF_WEEK, 6);
			// cDay.set(Calendar.DAY_OF_WEEK, 1);
		}
		System.out.println(DateUtil.formatDate(cDay.getTime()));
	}

	/**
	 * 获取月第一天
	 * 
	 * @return
	 */
	public static String fromatStringToDayOfMonth() {
		Calendar cDay = Calendar.getInstance();
		// 本月第一天
		cDay.setTime(new Date());
		cDay.set(Calendar.DAY_OF_MONTH, 1);
		return DateUtil.formatDate(cDay.getTime());
	}

	/**
	 * 获取月最后一天
	 * 
	 * @return
	 */
	public static String fromatStringToFirstDayOfMonth() {
		Calendar cDay = Calendar.getInstance();
		// 本月最后一天
		cDay.setTime(new Date());
		cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
		return DateUtil.formatDate(cDay.getTime());
	}

	/**
	 * 获取本月所有日期
	 * 
	 * @param date
	 * @return
	 */
	public static List<DateBean> getAllTheDateOftheDay() {
		List<DateBean> list = new ArrayList<DateBean>();
		Calendar start = Calendar.getInstance();
		Map<String, String> map = SystemConfiguration.getMap();
		String[] gameReviewStartTime = map.get("game_review_start_time").split("-");
		start.set(Integer.parseInt(gameReviewStartTime[0]), Integer.parseInt(gameReviewStartTime[1]),
				Integer.parseInt(gameReviewStartTime[2]));
		Long startTIme = start.getTimeInMillis();
		Calendar end = Calendar.getInstance();
		String[] gameReviewEndTime = map.get("game_review_end_time").split("-");
		end.set(Integer.parseInt(gameReviewEndTime[0]), Integer.parseInt(gameReviewEndTime[1]),
				Integer.parseInt(gameReviewEndTime[2]));
		Long endTime = end.getTimeInMillis();
		Long oneDay = 1000 * 60 * 60 * 24l;
		Long time = startTIme;
		Date currentDate = formatStringToDate(formatDate(new Date()) + " 00:00:00");
		while (time <= endTime) {
			Date date = new Date(time);
			DateBean dateBean = new DateBean();
			if (date.getTime() < currentDate.getTime()) {
				dateBean.setStartDate(DateUtil.formatDate(date));
				dateBean.setEndDate(DateUtil.formatDate(date));
				dateBean.setType("1");
				list.add(dateBean);
			}
			time += oneDay;
		}
		Collections.reverse(list);
		return list;
	}

	/**
	 * 获取 所有月份
	 * 
	 * @return
	 */
	public static List<DateBean> getAllTheDateOftheMonth() {
		List<DateBean> list = new ArrayList<DateBean>();
		Calendar start = Calendar.getInstance();
		Map<String, String> map = SystemConfiguration.getMap();
		String[] gameReviewStartTime = map.get("game_review_start_time").split("-");
		start.set(Integer.parseInt(gameReviewStartTime[0]), Integer.parseInt(gameReviewStartTime[1]),
				Integer.parseInt(gameReviewStartTime[2]));
		Long startTIme = start.getTimeInMillis();
		Calendar end = Calendar.getInstance();
		String[] gameReviewEndTime = map.get("game_review_end_time").split("-");
		end.set(Integer.parseInt(gameReviewEndTime[0]), Integer.parseInt(gameReviewEndTime[1]),
				Integer.parseInt(gameReviewEndTime[2]));
		Long endTime = end.getTimeInMillis();
		Long oneDay = 1000 * 60 * 60 * 24l;
		Long time = startTIme;
		while (time <= endTime) {
			Date date = new Date(time);
			DateBean dateBean = new DateBean();
			Calendar cDay = Calendar.getInstance();
			// 本月第一天
			cDay.setTime(date);
			cDay.set(Calendar.DAY_OF_MONTH, 1);
			dateBean.setStartDate(DateUtil.formatDate(cDay.getTime()));

			cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
			dateBean.setEndDate(DateUtil.formatDate(cDay.getTime()));
			dateBean.setType("3");
			if (!list.contains(dateBean)) {
				list.add(dateBean);
			}
			time += oneDay;
		}
		return list;
	}

	public static Date format(String time, String format) {
		SimpleDateFormat sdfhsm = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = sdfhsm.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String nowHHmmss() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		return sdf.format(date);
	}

	/**
	 * 获取每周日期
	 * 
	 * @param date
	 * @return
	 */
	public static List<DateBean> getAllTheDateOftheWEEK() {
		List<DateBean> list = new ArrayList<DateBean>();
		Calendar start = Calendar.getInstance();
		Map<String, String> map = SystemConfiguration.getMap();
		String[] gameReviewStartTime = map.get("game_review_start_time").split("-");
		start.set(Integer.parseInt(gameReviewStartTime[0]), Integer.parseInt(gameReviewStartTime[1]),
				Integer.parseInt(gameReviewStartTime[2]));
		Long startTIme = start.getTimeInMillis();
		Calendar end = Calendar.getInstance();
		String[] gameReviewEndTime = map.get("game_review_end_time").split("-");
		end.set(Integer.parseInt(gameReviewEndTime[0]), Integer.parseInt(gameReviewEndTime[1]),
				Integer.parseInt(gameReviewEndTime[2]));
		Long endTime = end.getTimeInMillis();
		Long oneDay = 1000 * 60 * 60 * 24l;
		Long time = startTIme;
		List<String> startList = new ArrayList<String>();
		Date currentDate = formatStringToDate(formatDate(new Date()) + " 00:00:00");
		while (time <= endTime) {
			Date date = new Date(time);
			DateBean dateBean = new DateBean();
			Calendar cDay = Calendar.getInstance();
			cDay.setTime(date);
			if (date.getTime() < currentDate.getTime()) {
				cDay.set(Calendar.DAY_OF_WEEK, 2);// 老外将周日定位第一天，周一取第二天
				dateBean.setStartDate(DateUtil.formatDate(cDay.getTime()));
				if (Calendar.DAY_OF_WEEK == cDay.getFirstDayOfWeek()) { // 如果刚好是周日，直接返回
					dateBean.setStartDate(DateUtil.formatDate(cDay.getTime()));
				} else {// 如果不是周日，加一周计算
					cDay.add(Calendar.DAY_OF_YEAR, 7);
					cDay.set(Calendar.DAY_OF_WEEK, 1);
				}
				startList.add(DateUtil.formatDate(cDay.getTime()));
				dateBean.setEndDate(DateUtil.formatDate(cDay.getTime()));
				dateBean.setType("2");
				if (!list.contains(dateBean)) {
					list.add(dateBean);
				}
			}
			time += oneDay;
		}
		Collections.reverse(list);
		return list;
	}

}
